package com.ydcrm.security.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.sun.xml.internal.ws.api.ha.StickyFeature;

/**
 * 用户模型
 * @author von_neumann
 *
 */
public class User extends Model<User>{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7695196582102922169L;
	public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024;
	private static final int SALT_SIZE = 8;
	public static final User dao = new User();
	
	public Page<User> paginate(int pageNumber,int pageSize,User user){
		StringBuilder from = new StringBuilder("from sec_user u left join sec_org o on u.org=o.id where 1=1");
		List<String> params = new ArrayList<String>();
		String username = user.getStr("username");
		String fullname = user.getStr("fullname");
		if(StringUtils.isNotEmpty(username)){
			from.append("and u.username=? ");
			params.add(username);
		}
		if(StringUtils.isNotEmpty(fullname)){
			from.append(" and u.fullname=? ");
			params.add(fullname);
		}
		from.append(" order by id desc ");
		return paginate(pageNumber, pageSize, "select u.*,o.name as orgName",from.toString(),params.toArray());
	}
	
	public User getByName(String name){
		return User.dao.findFirst("select u.*, o.name as orgName from sec_user u left join sec_org o on u.org=o.id where u.username=?",name);
	}
	
	public User get(Long id){
		return User.dao.findFirst("select u.*, o.name as orgName from sec_user u left join sec_org o on u.org=o.id where u.id=?",id);
	}
	
	public List<User> getByOrg(Integer orgId){
		String sql = "select u.*,o.name as orgName from sec_user u left join sec_org o on u.org=o.id";
		if(orgId != null && orgId >0){
			sql += " where u.org ="+orgId;
		}
		return User.dao.find(sql);
	}
	


}
